package com.cetc.stream;

import com.cetc.lambda.Person;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Classname StreamTest05
 * @Description Stream终止操作
 * @Date 2023/1/17 13:09
 * @Created by louzhuangxi
 */
public class StreamTest05 {
    List<Person> personList = Arrays.asList(
            new Person("张三", 24),
            new Person("李四", 80),
            new Person("王五", 28),
            new Person("赵六2", 42),
            new Person("赵六1", 42),
            new Person("田七", 28)
    );

    /**
     * 匹配与查找
     */
    @Test
    public void test01() {
        System.out.println("**********************检查是否匹配所有元素**********************");
        boolean flag = personList.stream().allMatch(t -> t.getName().contains("赵"));
        System.out.println(flag);

        System.out.println("**********************检查是否至少匹配一个元素********************");
        boolean flag1 = personList.stream().anyMatch(t -> t.getName().contains("赵"));
        System.out.println(flag1);

        System.out.println("**********************检查是否没有匹配所有元素**********************");
        boolean flag3 = personList.stream().noneMatch(t -> t.getName().contains("赵"));
        System.out.println(flag3);

        System.out.println("**********************返回第一个元素*******************************");
        String s = personList.stream().findFirst().toString();
        System.out.println(s);

        System.out.println("**********************返回当前流中的任意元素*******************************");
        String s1 = personList.stream().findAny().toString();
        System.out.println(s1);

        System.out.println("**********************返回流中元素总数*******************************");
        long count = personList.stream().count();
        System.out.println(count);

        System.out.println("**********************返回流中最大值*******************************");
        int maxAge = personList.stream().map(t -> t.getAge()).max(Integer::compareTo).get();
        System.out.println(maxAge);

        System.out.println("**********************返回流中最小值*******************************");
        int minAge = personList.stream().map(Person::getAge).min(Integer::compareTo).get();
        System.out.println(minAge);

        System.out.println("**********************内部迭代*******************************");
        personList.stream().map(Person::getAge).forEach(System.out::println);
    }
}
